package java8.stream.demo04mapstream;

import java8.stream.demo01.AbstractDishBeforeTest;
import java8.stream.demo01.Dish;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

/**
 * @author ChangLiang
 * @date 2020/4/22
 */
public class AppTest extends AbstractDishBeforeTest {

    @Test
    @DisplayName("test stream map")
    public void test(){
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 6, 7, 7, 1);
        System.out.println(list.stream().map(i -> i * 2).collect(toList()));
    }

    @Test
    @DisplayName("test stream map 2")
    public void test2(){
        System.out.println(menu.stream().map(Dish::getName).collect(toList()));
    }

    @Test
    @DisplayName("test stream flat map")
    public void test3(){
        String[] words = {"Hello", "World"};
        // remove duplicated character

        // Stream<String[]> {h,e,l,l,o},{W,o,r,l,d}
        Stream<String[]> stream = Arrays.stream(words).map(w -> w.split(""));
        // H,e,l,l,o,W,o,r,l,d
        Stream<String> stringStream = stream.flatMap(Arrays::stream);
        List<String> collect = stringStream.distinct().collect(toList());
        System.out.println(collect);

        List<String> collect1 = Arrays.stream(words).map(w -> w.split("")).flatMap(Arrays::stream).distinct().collect(toList());
        collect1.forEach(System.out::println);
    }
}
